<?php
 Class BrandAction extends Action{
 	public function index(){
 		$db=M();
 		$sql1="select count(*) tot from brand,class where brand.class_id=class.id";
 		$row=$db->query($sql1);
 		$count=$row[0]['tot'];
 		Import('ORG.Util.Page');
 		$page=new Page($count,10);
 		$show=$page->show();
 		$sql2="select b.*,c.name as cname from brand as b left join class as c on b.class_id=c.id order by b.id";
 		$rows=$db->query($sql2);
 		$this->assign('show',$show);
 		$this->assign('rows',$rows);
 		$this->display();
 	}
 	// 添加模版
 	public function add(){
 		$class=M('Class');
 		$classrows=$class->select();
 		$this->assign('classrows',$classrows);
 		$this->display();
 	}
 	// 添加操作
 	public function add_handle(){
 		$brand=M('Brand');
 		if($brand->create()){
 			// echo '<pre>';
 			// print_r($_POST);exit;
 			$brand->add();
 			$this->redirect('index');
 		}else{
 			$this->error('添加失败');
 		};
 	}

	// 删除
	 	public function delete(){
	 		$brand=M('Brand');
	 		// 获取$.get传过来的id
	 		$id=$_GET['id'];
			$re=$brand->delete($id);
			if($re){
				$arr['status']=1;
				$arr['info']='删除成功';
				echo json_encode($arr);
			}
	 		
	 		
	 	}


 	// 修改模版
 	public function edit(){
 		// 查出相应条数品牌信息
 		$brand=M('Brand');
 		$id=$_GET['id'];
 		$brandrow=$brand->find($id);

 		// 显示所有分类信息
 		$class=M('Class');
 		$classrows=$class->select();

 		$this->assign('brandrow',$brandrow);
 		$this->assign('classrows',$classrows);
 		$this->display();
 	}
	// 修改操作
 	public function edit_handle(){
 		$brand=M('Brand');
 		if($brand->create()){
 			$brand->save();
 			$this->redirect('index');
 		};
 	}
 }
?>